<?php

/**
 * Grafico Google
 *
 * Por ahora funciona como extensión de componente Table_Dojo del cual hereda el datastore, típicamente mediante popup.<br/>
 * No funciona como stand alone.<br/>
 * Ejemplo:
 * <code>
 * <section name="Gráfico de Ganadores" type="google_chart" inherits_data="true" 
 * cols="nombre" titles="fecha_hora" items="cantidad" list="false" edit="false" add="false"
 * delete="false"/>
 * </code>
 *
 * @category Zwei
 * @package Zwei_Admin
 * @subpackage Components
 * @version $Id:$
 * @since 0.1
 *
 */

class Zwei_Admin_Components_GoogleChart
{
    /**
     * 
     * @var String
     */
    public $page;
    /**
     * 
     * @var Zwei_Admin_Acl
     */
    private $_acl;
    /**
     * 
     * @var Zwei_Db_Table
     */
    private $_model;
    /**
     * 
     * @param string
     */ 
    function __construct($page){
        $this->page = $page;
        $userInfo = Zend_Auth::getInstance()->getStorage()->read();
        $this->_acl = new Zwei_Admin_Acl($userInfo->user_name);
    }

    /**
     * Despliegue para mostrar listados
     * @return HTML
     */

    function display()
    {
        $oForm = new Zwei_Utils_Form();
         
        $oViewtable = new Zwei_Admin_Components_Helpers_ViewTable($this->page);
        $oViewtable->getLayout();
         
        $out = "    <script type=\"text/javascript\" src=\"https://www.google.com/jsapi\"></script>
    <script type=\"text/javascript\">
      try{
            window.parent.document.getElementById('popup_body').style.backgroundColor='#ffffff';
      }catch(e){}

      
      google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.axisTitlesPosition='in';
        data.addColumn('string', 'Columna');\n";
            
        
        $sStoreUri = html_entity_decode(urldecode($oForm->uri));
        if (isset($oViewtable->layout[0]['INHERITS_DATA'])) {

            $aUri = parse_url($sStoreUri);
            parse_str($aUri['query'], $aParams); 
            $oSubForm = new Zwei_Utils_Form($aParams);//Se convierte array a Zwei_Utils_GForm
            $model = Zwei_Utils_String::toClassWord($oSubForm->model)."Model";
            $this->_model = new $model;
            $oDbObject = new Zwei_Db_Object($oSubForm);
            $oSelect = $oDbObject->select();
            $oCollection = $this->_model->fetchAll($oSelect); 

                
            $rows = $oCollection->count();
    
            $aPrintedCols = array();
            $aPrintedTitles = array();
                 
            $values = '';
            $i = 1;
            $j = 0;
            foreach ($oCollection as $v) {
                $column = $v[$oViewtable->layout[0]['COLS']];
                if (!in_array($column, $aPrintedCols)) {
                    $out .= "\tdata.addColumn('number', '$column');\n";
                    $aPrintedCols[$i]=$column;
                    $i++;
                    //$j=0;
                } else {
                    $j++;
                }
    
                if (!in_array($v[$oViewtable->layout[0]['TITLES']], $aPrintedTitles)) {
                    $values .= "\tdata.setValue($j, 0, '{$v[$oViewtable->layout[0]['TITLES']]}');\n";
                    $aPrintedTitles[]=$v[$oViewtable->layout[0]['TITLES']];
                }
    
                $index = array_keys($aPrintedCols, $column);
                $values .= "\tdata.setValue($j, {$index[0]}, {$v[$oViewtable->layout[0]['ITEMS']]});\n";
            }
                 
    
            $out .= "\tdata.addRows($rows);\n";
            $out .= $values;
            

        } else {
            
            
            /*[TODO]
             * Aca debiera ir la logica en caso de que no herede datos de URL,
             * usar clase modelo enviada en $viewtable->layout[0]['TARGET']
             */
        }
         

         

        $out .= "
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        var options = {$oViewtable->layout[0]['OPTIONS']};
        
        chart.draw(data, options);
      }
    </script>
    <div id=\"chart_div\"></div>

        ";
      
        return $out;
    }
}
?>